<?php
defined('IN_APPS') or die('No direct access!');

/**
 * Main model for our apps. All other model will
 * extends this model
 */  
class Model {
	
	// our default connection
	private $con;
	
	
	/**
	 * Class constructor
	 */	 	
	public function __construct()
	{
		// assign tablename
		$this->tableName = $this->name;
		
		// connect to MySQL
		$this->connect();
	}
	
	
	/**
	 * Class destructor
	 */	 	
	public function __destruct()
	{
		// disconnect MySQL connection
		$this->disconnect();
	}
	
	
	/**
	 * Create MySQL connection
	 */	 	
	private function connect()
	{
		// load our configuration
		global $config;
		
		if (!$this->con) {
			$this->con = mysql_connect($config['db_host'], $config['db_user'], $config['db_pass']);

			// if we successfully establish connection
			if ($this->con) {

				// select which db we want to use
				mysql_select_db($config['db_name'], $this->con);
			}
		}
	}
	
	
	/**
	 * Close MySQL connection
	 */
	private function disconnect()
	{
		if (is_resource($this->con)) {
			mysql_close($this->con);
		}
	}
	
	
	/**
	 * Get all record from database
	 */
	public function getAll()
	{
		$sql = 'SELECT * FROM `' . $this->tableName . '`';
		$query = mysql_query($sql);
		
		$result = array();
		
		if ($query) {
			while ($row = mysql_fetch_array($query)) {
				$result[] = $row;
			}
		}
		
		return $result;
	}
	
	
	/**
	 * Get single record from database
	 */
	public function get($id)
	{
		$sql = 'SELECT * FROM `' . $this->tableName . '` WHERE id = ' . $id;
		$query = mysql_query($sql);

		if ($query) {
			$result = mysql_fetch_array($query);
			return $result;
		}
		else {
			die('Error: ' . mysql_error());
		}
	} 	
	
	
	/**
	 * Create new record
	 */
	public function create($data)
	{
		// array to hold fieldnames
		$fields = array();
		
		// array to hold values
		$values = array();
		
		// split data into 2 section; fieldname and value
		foreach ($data as $key => $value) {
			// assign key to array
			$fields[]	= "`" . $key . "`";
			
			// assign value to array
			// we need to add quote to the values
			$values[]	= "'" . $value . "'";
		}

		$sql = "INSERT INTO `" . $this->tableName . "` (" . implode(', ', $fields) . ") " .
			   "VALUES (" . implode(', ', $values) . ")";
		
		// run the query
		$query = mysql_query($sql);
		
		// check if the process successful
		if ($query) {
			return true;
		}
		else {
			die('Error:' . mysql_error());
		}
	}
	
	
	/**
	 * Update record
	 */
	public function update($id, $data)
	{
		// array to hold fieldnames and values
		$fields = array();
		
		// split data into 2 section; fieldname and value
		foreach ($data as $key => $value) {
			// assign key to array
			$fields[]	= $key . " = '" . $value . "'";
		}
		
		$sql = "UPDATE " . $this->tableName . " SET " . implode(', ', $fields) . " " .
			   "WHERE id = " . $id;
		
		// run the query
		$query = mysql_query($sql);
		
		// check if the process successful
		if ($query) {
			return true;
		}
		else {
			die('Error:' . mysql_error());
		}
	}
	
	
	/**
	 * Delete record based on ID
	 * 
	 * @param	integer		$id		Record ID	 	 
	 */
	public function del($id)
	{
		$sql = 'DELETE FROM `' . $this->tableName . '` WHERE id = ' . $id;
		$query = mysql_query($sql);
		
		if ($query) {
			return true;
		}
		else {
			return mysql_error();
		}
	} 
	
}